package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;

    @Autowired
    private UserAccountDetailMapper userAccountDetailMapper;

    @Override
    public void initUserAccount(Long userId) {
        UserAccount userAccount = new UserAccount();
        userAccount.setUserId(userId);
        userAccount.setTotalAmount(new BigDecimal(10));
        userAccount.setAvailableAmount(new BigDecimal(10));
        userAccount.setTotalIncomeAmount(new BigDecimal(10));
        userAccount.setTotalPayAmount(new BigDecimal(0));
        userAccountMapper.insert(userAccount);
        this.saveUserAccountDetail(userId, "初始化用户账户", SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT, new BigDecimal(10), "");
    }

    /**
     * 新增账户变动日志
     * @param userId 用户ID
     * @param title 资金变动缘由
     * @param tradeType 交易类型
     * @param amount 变动金额
     * @param orderNo 订单编号
     */
    @Override
    public void saveUserAccountDetail(Long userId, String title, String tradeType, BigDecimal amount, String orderNo) {
        UserAccountDetail userAccountDetail = new UserAccountDetail();
        userAccountDetail.setUserId(userId);
        userAccountDetail.setTitle(title);
        userAccountDetail.setTradeType(tradeType);
        userAccountDetail.setAmount(amount);
        userAccountDetail.setOrderNo(orderNo);
        userAccountDetailMapper.insert(userAccountDetail);
    }

    /**
     * 检查及锁定账户金额
     * @param accountLockVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void checkAndLock(AccountLockVo accountLockVo) {
        BigDecimal amount = accountLockVo.getAmount();
        Long userId = accountLockVo.getUserId();
        String orderNo = accountLockVo.getOrderNo();
        String content = accountLockVo.getContent();
        //检查账号余额
        UserAccount userAccount = userAccountMapper.selectById(userId);
        if (userAccount.getAvailableAmount().compareTo(amount) < 0){
            throw new GuiguException(400, "余额不足！");
        }
        //扣减金额
        userAccountMapper.subtractAvailableAmount(userId, amount);
        //添加明细
        UserAccountDetail userAccountDetail = new UserAccountDetail();
        userAccountDetail.setUserId(userId);
        userAccountDetail.setTitle(content);
        userAccountDetail.setTradeType(SystemConstant.ACCOUNT_TRADE_TYPE_MINUS);
        userAccountDetail.setAmount(amount);
        userAccountDetail.setOrderNo(orderNo);
        userAccountDetailMapper.insert(userAccountDetail);
    }

    /**
     * 查询用户账户明细
     * @param userAccountDetailPage
     * @param userId
     * @return
     */
    @Override
    public Page<UserAccountDetail> findUserAccountDetailPage(Page<UserAccountDetail> userAccountDetailPage, Long userId, String tradeType) {
        LambdaQueryWrapper<UserAccountDetail> userAccountDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userAccountDetailLambdaQueryWrapper.eq(UserAccountDetail::getUserId, userId);
        userAccountDetailLambdaQueryWrapper.eq(UserAccountDetail::getTradeType, tradeType);
        userAccountDetailLambdaQueryWrapper.orderByDesc(UserAccountDetail::getCreateTime);
        return userAccountDetailMapper.selectPage(userAccountDetailPage, userAccountDetailLambdaQueryWrapper);
    }
}
